/**
 * 
 */
package com.zufangbao.earth.web.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Secure;
import com.zufangbao.earth.RoleSpec;
import com.zufangbao.earth.ViewEntry;
import com.zufangbao.earth.entity.security.Principal;
import com.zufangbao.earth.handler.CookieHandler;
import com.zufangbao.gluon.spec.earth.GlobalSpec4Earth;

/**
 * @author lute
 *
 */

@Controller
@RequestMapping("")
public class HomeController {
	
	@Autowired
	private CookieHandler cookieHandler;
	/**
	 * Redirect to app renters
	 * 
	 * @return
	 */
	@RequestMapping("/data")
	public String data(@Secure Principal principal) {
		
		if(principal.getAuthority().equals(RoleSpec.ROLE_SUPER_USER)){
			
			return "redirect:/renters";
			
		}else{
			
			return "redirect:/contract-partical/partical-contracts-list";
		}
		
	}
	
	@RequestMapping("/finance")
	public String finance() {
		return "redirect:/flow/receipt";
	}
	
	/**
	 * Redirect to service provider
	 * 
	 * @return
	 */
	@RequestMapping("/system")
	public String system() {
		return "redirect:/post-update-password";
	}
	
	@RequestMapping("/app")
	public String app() {
		return "redirect:/flow/receipt";
	}
	
	@RequestMapping("/institution")
	public String institution() {
		return "redirect:/distribution";
	}
	@RequestMapping("/receivables")
	public String receivable(HttpServletResponse response,@Secure Principal principal){
		
		if(principal.getAuthority().equals(RoleSpec.ROLE_SUPER_USER)){
			
			return "redirect:/setting/app-brief-list?targetUrl=/receivables/receivables-summary";
			
		}
		ViewEntry viewCategory = new ViewEntry(principal.getName(), principal.getAuthority());
		
		cookieHandler.addCookie(GlobalSpec4Earth.PARAM_VIEW_CATEGORY, JsonUtils.toJsonString(viewCategory), response);
			
		return "redirect:/receivables/receivables-summary";
	}
	
}
